//
//  NSString+Time.m
//  XKJY
//
//  Created by xiaokang100 on 2017/1/10.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "NSString+Time.h"
#import "NSDate+YGCategory.h"

@implementation NSString (Time)
+ (NSString *)getCurrentTimeString {
    return [self getCurrentTimeStringWithFormatter:@"YYYY-MM-dd HH:mm:ss"];
}

+ (NSString *)getCurrentTimeStringWithFormatter:(NSString *)formatterString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    // 设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:formatterString];
    //    // 设置时区,这个对于时间的处理有时很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//现在时间
    return [formatter stringFromDate:date].copy;
}

+ (NSString *)getTimeStringWithTimestamp:(NSInteger)timestamp {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    // 时间戳转时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    return [dateFormatter stringFromDate:confromTimesp].copy;
}

+ (NSString *)getTimeStringWithTimestamp:(NSInteger)timestamp withFormatter:(NSString *)formatterString {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:formatterString];
    // 时间戳转时间
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    return [dateFormatter stringFromDate:confromTimesp].copy;
}

+ (NSString *)getTimestamp {
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval interval = [date timeIntervalSince1970];
    return [[self stringWithFormat:@"%0.f", interval] copy];
}

+ (NSString *)calculateTimeGapWitNewTimestamp:(NSString *)newTimestamp oldTimestamp:(NSString *)oldTimestamp {
    
    NSInteger timeInteger = newTimestamp.integerValue - oldTimestamp.integerValue;
    NSInteger year = timeInteger / (60*60*24*365);
    NSInteger day  = (timeInteger - year*(60*60*24*365)) / (60*60*24);
    NSInteger hour = (timeInteger - day*(60*60*24)) / (60*60);
    NSInteger min  = (timeInteger - hour*(60*60)) / (60);
    NSInteger sec  = timeInteger % (60);
    
    if(year == 0 && day == 0 && hour == 0 && min == 0) {
        return [self stringWithFormat:@"%zd秒",sec];
    } else if (year == 0 && day == 0 && hour == 0) {
        return [self stringWithFormat:@"%zd分%zd秒",min,sec];
    } else if (year == 0 && day == 0) {
        return [self stringWithFormat:@"%zd时%zd分%zd秒",hour,min,sec];
    } else if (year == 0) {
        return [self stringWithFormat:@"%zd天%zd时%zd分%zd秒",day,hour,min,sec];
    } else {
        return [self stringWithFormat:@"%zd年%zd天%zd时%zd分%zd秒",year,day,hour,min,sec];
    }
    
}

+ (NSString *)intervalSinceNowFrome:(NSInteger)timeStamp {
    // 输入时间
    NSDate *fromDate = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    // 获取当前时间
    NSDate *adate = [NSDate date];
    // 计算时间差
    NSInteger intervalTime = [adate timeIntervalSinceDate:fromDate];
    
    NSInteger minutes   = intervalTime / 60;
    NSInteger hours     = intervalTime / 60 / 60;
    NSInteger day       = intervalTime / 60 / 60 / 24;
//    NSInteger month     = intervalTime / 60 / 60 / 24 / 30;
//    NSInteger yers      = intervalTime / 60 / 60 / 24 / 365;
    
    if (minutes <= 1) {
        return  @"刚刚";
    }else if (minutes < 60){
        return [NSString stringWithFormat: @"%ld分前",(long)minutes];
    }else if (hours < 24){
        return [NSString stringWithFormat: @"%ld时前",(long)hours];
    }else if (day < 2){
        return [NSString stringWithFormat: @"%ld天前",(long)day];
    }else if (day >= 2 && day < 30 && [fromDate isThisYear]){
        NSDateFormatter * df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"MM月dd日 HH:mm:ss";
        NSString * time = [df stringFromDate:fromDate];
        return time;
    }else{
        NSDateFormatter * df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";
        NSString * time = [df stringFromDate:fromDate];
        return time;
    }
    return @"";
}


@end
